﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace FileListMaker
{
    public partial class ResultForm : Form
    {
        public ResultForm()
        {
            InitializeComponent();
        }

        private int listCount = 0;
        private string listCountTemplate = "ListCount: {0}";
        private string findingStatusTemplate = "Finding... : {0}";
        private IAsyncListService service;

        public void ShowFileListResult(string rootDirPath, IListCondition cond)
        {
            lblListCount.Text = listCountTemplate.Replace("{0}", listCount.ToString());
            Show();
            service = new AsyncDefaultListService(cond);
            service.FindFile += service_FindFile;
            service.FinishFind += service_FinishFind;
            service.GetResultAsync(rootDirPath);
        }

        private string resultCache = "";

        void service_FindFile(object sender, FindFileEventArgs e)
        {
            this.Invoke((MethodInvoker)delegate()
            {
                lblFindingStatus.Text = findingStatusTemplate.Replace("{0}", e.FileFullName);
                resultCache += e.FileFullName + System.Environment.NewLine;
                lblListCount.Text = listCountTemplate.Replace("{0}", (++listCount).ToString());
            });
        }

        void service_FinishFind(object sender, FinishFindEventArgs e)
        {
            this.Invoke((MethodInvoker)delegate()
            {
                this.txtResult.Text = resultCache;
            });
        }

        private int ajastAreaWidth = 34;

        private void ResultForm_SizeChanged(object sender, EventArgs e)
        {
            lblFindingStatus.Width = this.Width - lblListCount.Width - ajastAreaWidth;
        }

        private void ResultForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            service.FindFile -= service_FindFile;
            service.FinishFind -= service_FinishFind;
            service.Stop();
        }
    }
}
